package com.gientech.service.impl;

import com.gientech.entity.UserInfo;
import com.gientech.exception.Asserts;
import com.gientech.mapper.UserInfoMapper;
import com.gientech.service.UserService;
import com.gientech.utils.EncryptUtil;
import com.gientech.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author : 钟鸣星
 * @date : 2023年01月01日
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Override
    public String login(String username, String password) {
        String token = null;
        //密码需要客户端加密后传递
        UserInfo userInfo = userInfoMapper.selectByUserName(username);

        boolean isNotMatchPassword = !EncryptUtil.encryptByDes(password).equals(userInfo.getPassword());
        if(isNotMatchPassword){
            Asserts.fail("密码不正确");
        }

        token = jwtTokenUtil.generateToken(userInfo);
        return token;
    }

    @Override
    public UserInfo getUserInfoByUsername(String username) {
        return userInfoMapper.selectByUserName(username);
    }
}
